<template>
  <div :id="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/util/util'

export default {
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '100px'
    },
    className: {
      type: String,
      default: 'chart'
    },
    centerColor: {
      type: String,
      default: '#ffffff'
    },
    visitorData: {
      type: Array,
      default: () => ([])
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const that = this
      let total = 0
      this.visitorData.forEach(item => {
        total += item.value
      });
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        title: {
          text: total,
          left:'17%',
          top:'41%',
          textStyle:{
            fontSize: 16,
            color: that.centerColor,
            align:'center'
          }
        },
        tooltip: {
            trigger: 'item'
        },
        grid: {
          top: 10,
          left: 10,
        },
        legend: {
          type: 'plain',
          orient: 'vertical',
          left: '45%',
          top: 'center',
          align: 'left',
          itemGap: 6,
          itemWidth: 10, // 设置宽度
          itemHeight: 10, // 设置高度
          icon:'rect',
          symbolKeepAspect: false,
          formatter: function (name) {
            let data = that.visitorData
            let total = 0
            let tarValue
            for (let i = 0; i < data.length; i++) {
              total += data[i].value
              if (data[i].name == name) {
                tarValue = data[i].value
              }
            }
            let v = Math.trunc((tarValue/total)*100)
            let resoult = '{a|'+name+' | }'+'{b|'+v+'%  }'+'{a|'+tarValue+'}'
            if (that.centerColor !== '#ffffff') {
              resoult = '{a|'+name+' (}'+'{b|'+tarValue+')  }'+'{a|'+v+'%}'
            }
           return resoult
          },
          textStyle:{
            rich:{
              a:{
                color:'#ffffff',
                fontSize:14,
              },
              b:{
                color:'#ffffff',
                fontSize:14,
              }
            }
          }
        },
        series: [
            {
              type: 'pie',
              radius: ['40%', '70%'],
              center: ['25%', '50%'],
              avoidLabelOverlap: false,
              label: {
                show: false,
                position: 'left'
              },
              labelLine: {
                show: false
              },
              data: this.visitorData
            }
        ]
      })
    }
  }
}
</script>
